package com.forekenow.Demo14;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 预处理（接口 PrepareStatement）：主要用于执行动态的SQL语句（id=？ and name=？）
 */
public class PrepareStatementDemo {
    public static void main(String[] args) {
        //1.加载驱动    jdbc驱动类：com.mysql.jdbc.Driver 8.0改成com.mysql.cj.jdbc.Driver
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/basic5?useSSL=false&useUnicode=true&characterEncoding=utf-8",
                    "root","root");
            //3.获取PreparedStatement对象用于执行动态ＳＱＬ
//            PreparedStatement pstmt = conn.prepareStatement("insert into guestbook(name,phone,email,title,content)value (?,?,?,?,?)");
//            //将？替换成具体的值
//            pstmt.setString(1,"fender");
//            pstmt.setString(2,"123123123123");
//            pstmt.setString(3,"adfjklsdjfl@133.com");
//            pstmt.setString(4,"title");
//            pstmt.setString(5,"test");
//            int i = pstmt.executeUpdate();
//            System.out.println(i);

            PreparedStatement pstmt1 = conn.prepareStatement("update guestbook set phone=? where id =?");
            pstmt1.setString(1,"title");
            pstmt1.setString(2,"2");
            pstmt1.executeUpdate();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }
}
